package servlet;

import Dao.StudentDao;
import Dao.UserDao;
import Impl.StudentDaoImpl;
import com.google.gson.Gson;
import model.Result;
import model.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/RegisterStudentServlet2")
public class RegisterStudentServlet2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");

        String stuNo = request.getParameter("stuNo");
        String stuName = request.getParameter("stuName");
        String userName = request.getParameter("userName");

        if (stuNo != null && stuName != null) {
            StudentDaoImpl studentDao=new StudentDao();
            Student student=new Student(stuNo,stuName);

            PrintWriter writer=response.getWriter();
            Gson gson=new Gson();
            boolean result=studentDao.saveStudent(student);
            if(result){
                UserDao userDao=new UserDao();
                userDao.setStuNo(userName,stuNo);
                writer.write(gson.toJson(new Result("ok")));
            }
            else
                writer.write(gson.toJson(new Result("false")));


        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
